/**
 * 
 */
package com.nexttwist;

import java.util.ArrayList;
import java.util.List;

import android.app.Dialog;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.Toast;

import com.nexttwist.adapters.CommentsAdapter;
import com.nexttwist.adapters.DashboardAdapter;
import com.nexttwist.dao.CommentsDAO;
import com.nexttwist.model.Comment;
import com.nexttwist.model.SampleTrack;

/**
 * @author HarishKumarG
 *
 */
public class DashboardFragment extends Fragment {
	
	//private View mView;
	private ListView mListView;
	private CommentsDAO commentDAO = new CommentsDAO();
	
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		View mView = inflater.inflate(R.layout.fragment_dashboard, container, false);
		
		mListView = (ListView) mView.findViewById(R.id.list_of_records);
		DashboardAdapter mAdapter = new DashboardAdapter(getActivity().getApplicationContext(), getDummyData());
		mListView.setAdapter(mAdapter);
		mListView.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
				Toast.makeText(getActivity().getApplicationContext(),"Click ListItem Number " + position, Toast.LENGTH_LONG).show();
				showCustomDialog();
			}
		});
		
		return mView;
	}
	
	
	private List<SampleTrack> getDummyData() {
		List<SampleTrack> list = new ArrayList<>();
		SampleTrack track;
		track = new SampleTrack();
		track.setAlbum("Album One");
		track.setLength(120);
		track.setThumbsDown(5);
		track.setThumbsDown(4);
		track.setTitle("My song one");
		list.add(track);
		
		track = new SampleTrack();
		track.setAlbum("Album One");
		track.setLength(130);
		track.setThumbsUp(6);
		track.setThumbsDown(5);
		track.setTitle("My song two");
		list.add(track);
		
		track = new SampleTrack();
		track.setAlbum("Album One");
		track.setLength(110);
		track.setThumbsUp(3);
		track.setThumbsDown(4);
		track.setTitle("My song three");
		list.add(track);
		
		track = new SampleTrack();
		track.setAlbum("Album Two");
		track.setLength(120);
		track.setThumbsUp(10);
		track.setThumbsDown(4);
		track.setTitle("My song one");
		list.add(track);
		
		track = new SampleTrack();
		track.setAlbum("Album Two");
		track.setLength(150);
		track.setThumbsUp(1);
		track.setThumbsDown(4);
		track.setTitle("My song two");
		list.add(track);
		return list;
	}

	private void showCustomDialog() {
        final Dialog dialog = new Dialog(getActivity());
        dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
        dialog.setContentView(R.layout.activity_track_detail);
        
        Button btn = (Button) dialog.findViewById(R.id.commentBtn);
        btn.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				showAddCommentDialog();
				
			}
		});
        
        List<Comment> commentList = commentDAO.getAllComments(getActivity());
        ListView comments = new ListView(getActivity());
        CommentsAdapter modeAdapter = new CommentsAdapter(getActivity(), commentList);
        comments.setAdapter(modeAdapter);
        
        dialog.show();
    }
	
	
	private void showAddCommentDialog(){
		final Dialog dialog = new Dialog(getActivity());
        dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
        dialog.setContentView(R.layout.save_comment_dialog);
        
        Button button = (Button)dialog.findViewById(R.id.saveComment);    
        button.setOnClickListener(new View.OnClickListener() {
            
            @Override
            public void onClick(View v) {
            	try {
            		EditText comment = (EditText) dialog.findViewById(R.id.comment);

					saveComment(comment.getText().toString());
	            	
	                dialog.dismiss();
            	} catch (Exception e) {
					e.printStackTrace();
				}
            }

        });
                
        
        dialog.show();
	}
	
	private void saveComment(String comment) {
		Comment commentObj = new Comment();
		commentObj.setTrackId(2);
		commentObj.setComment(comment);
		commentObj.setUserId(11);
		commentDAO.createComment(getActivity(), commentObj);
		
		List<Comment> comments = commentDAO.getAllComments(getActivity());
		System.out.println(comments.size());
	}
}
